package com.example.refresh;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;

import uk.co.imallan.jellyrefresh.JellyRefreshLayout;
import uk.co.imallan.jellyrefresh.PullToRefreshLayout;

public class JellyRefresh extends AppCompatActivity {
    private ListView listView;
    private JellyRefreshLayout jellyRefreshLayout;
    private ArrayList<String> arrayList = new ArrayList<>();
    private Handler handler;
    private int MSG_REFRESH = 1002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jelly_refresh);
        setTitle("其他刷新");

        listView = findViewById(R.id.listJelly);
        jellyRefreshLayout = findViewById(R.id.jelly);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(JellyRefresh.this,
                android.R.layout.simple_list_item_1,arrayList);
        listView.setAdapter(arrayAdapter);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what==MSG_REFRESH){
                    jellyRefreshLayout.setRefreshing(false);
                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
                return false;
            }
        });

        //刷新监听
       jellyRefreshLayout.setPullToRefreshListener(new PullToRefreshLayout.PullToRefreshListener() {
           @Override
           public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       for (int i=0;i<14;i++) {
                           arrayList.add("下拉刷新" + i);
                       }
                       try {
                           Thread.sleep(3000);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       handler.sendEmptyMessage(MSG_REFRESH);

                   }
               }).start();
           }
       });
    }
}
